package com.freesky.biz.psbc.controller;

import com.freesky.biz.psbc.domain.TPsbcParity;
import com.freesky.biz.psbc.domain.TPsbcParityItem;
import com.freesky.biz.psbc.domain.TPsbcStock;
import com.freesky.biz.psbc.service.IPsbcParityService;
import com.freesky.biz.psbc.service.IPsbcStockService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/psbc/stock")
public class PsbcStockController extends BaseController {

    @Autowired
    private IPsbcStockService psbcStockService;

    /**
     * 邮储券的封装
     */
    @PreAuthorize("@ss.hasPermi('psbc:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPsbcStock psbcStock) {
        startPage();
        List<TPsbcStock> list = psbcStockService.findList(psbcStock);
        return getDataTable(list);
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('psbc:stock:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable String configId) {
        TPsbcStock psbcStock = psbcStockService.get(configId);
        return success(psbcStock);
    }

    /**
     * 新增活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:stock:add')")
    @Log(title = "邮储券的封装", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TPsbcStock psbcStock) {
        if(StringUtils.isEmpty(psbcStock.getId())){
            return AjaxResult.error("ID号不能为空，请长度为10位以上!");
        }

        psbcStock.setId(psbcStock.getId().trim());
        psbcStock.setName(psbcStock.getName().trim());
        psbcStock.setCreateBy(String.valueOf(getUserId()));
        int nRows = psbcStockService.insert(psbcStock);
        TPsbcStock updatePsbcStock = psbcStockService.get(psbcStock.getId());
        psbcStockService.setPsbcStockToRedis("PSBC:COUPON:"+psbcStock.getId(),updatePsbcStock);
        return toAjax(nRows);
    }

    /**
     * 修改活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:stock:edit')")
    @Log(title = "邮储券的封装", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TPsbcStock psbcStock) {
        psbcStock.setId(psbcStock.getId().trim());
        psbcStock.setName(psbcStock.getName().trim());
        psbcStock.setUpdateBy(String.valueOf(getUserId()));
        int nRows = psbcStockService.update(psbcStock);
        TPsbcStock updatePsbcStock = psbcStockService.get(psbcStock.getId());
        psbcStockService.setPsbcStockToRedis("PSBC:COUPON:"+psbcStock.getId(),updatePsbcStock);
        return toAjax(nRows);
    }

    /**
     * 删除活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:stock:remove')")
    @Log(title = "邮储券的封装", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        psbcStockService.deleteByIds(configIds);
        return success();
    }

}
